package SA.Gms.Ui.Util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Date;

public class ReadTestSuite {

	/**
	 * @param args
	 */
	public static File directory = new File (".");
	public static String tcFilePath,xlfilepath,suitePath,Suitename,OutPath,testSuitePath,browsername;
	public static BrowserSetup Bsload;
	public static PrintWriter log1=null;
	public static void main(String[] param) {
		// TODO Auto-generated method stub
		BufferedReader br = null;
		String line;
		
		try {
			testSuitePath=directory.getCanonicalPath()+File.separator+"Suites"+File.separator+param[0];
			suitePath=testSuitePath+File.separator+param[0]+".txt";
			Suitename=param[0];
			System.setProperty("java.class.path",directory.getCanonicalPath()+File.separator+"src/lib");
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			
			br = new BufferedReader(new InputStreamReader(
					new FileInputStream(suitePath), "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			if(param.length>1)
			{
			xlfilepath=param[1];
			browsername=param[2];
			}
			else
			{
				xlfilepath=null;
				browsername=null;
				
			}
			String path=null,Status;

			try {
				System.out.println(directory.getCanonicalPath());
				path = directory.getCanonicalPath()+File.separator+"src"+File.separator+"lib"+File.separator;
				if (!new File(testSuitePath+"/logs/").isDirectory()) {
					new File(testSuitePath+"/logs/").mkdir();
				}
				if (!new File(testSuitePath+"/Results/").isDirectory()) {
					new File(testSuitePath+"/Results/").mkdir();
				}
				log1 = new PrintWriter(new OutputStreamWriter(
						new FileOutputStream(testSuitePath+"\\logs\\log_"+new Date().toString().replaceAll(":", "-")+".log",true), "UTF-8"));
				
				System.out.println(path);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			Bsload=new BrowserSetup(path,browsername);
			if (new File(testSuitePath+"/Results/"+ File.separator+ Suitename).isDirectory()) {
				File newDir = new File(new File(testSuitePath+"/Results/").getParent() + File.separator+ Suitename+System.currentTimeMillis());
				new File(testSuitePath+"/Results/"+ File.separator+ Suitename).renameTo(newDir);  
			}
			//new File(testSuitePath+"/Results/"+ File.separator+ Suitename).mkdir();
			OutPath=testSuitePath+"/Results/"+ File.separator+"/";
			System.out.println(OutPath);
			while ((line = br.readLine()) != null) {
				tcFilePath=testSuitePath+File.separator+"testcase"+File.separator+line+".xml";
				log1.println("Test Case Name : "+line);	
				ReadTestCase tc=new ReadTestCase(tcFilePath,xlfilepath);
				
			}
			
			log1.close();
			Bsload.exitTestcase();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
